# -*- coding: utf-8 -*-
import json
import time

from scrapy.http import Request
from scrapy.selector import Selector

from amac_spider.items import AoinProductItem
from amac_spider.utils.my_redis_spider import RedisSpiderPro
from amac_spider.utils.handler import get_url, handle_url, get_datetime, get_table_name, get_item_number_name


# 证券公司直投基金
class AoinProductSpider(RedisSpiderPro):
    name = 'aoin_product'

    def parse(self, response):
        item = AoinProductItem()
        info_list = json.loads(response.text)['content']
        for info in info_list:
            url = handle_url(get_url(self.name), info['id'])
            item['product_id'] = info['id']
            item['detail_url'] = url
            item['crawl_time'] = get_datetime(time.time())
            yield Request(url=url, callback=self.parse_detail, meta={'item': item}, dont_filter=True)

    def parse_detail(self, response):
        item = response.meta['item']
        fields = ['product_name', 'product_code', 'subsidiary', 'manager', 'found_time',
                  'record_time', 'fund_type', 'organization_shape', 'work_stat', 'trusteeship', 'trustee']
        selector = Selector(response)
        for index, field in enumerate(fields):
            item[field] = selector.xpath(f'//*[@class="table"]//tr[{index+1}]/td[2]/text()').extract_first()

        yield item

